Web Push Service
The Web Push Service is a core component of the SuperSet Telegram Notification Bot that enables real-time browser notifications through the Web Push protocol with VAPID (Voluntary Application Server Identification) authentication. This service integrates seamlessly with the multi-channel notification ecosystem alongside Telegram notifications, providing users with instant updates delivered directly to their web browsers.
The service implements a comprehensive notification delivery mechanism that includes subscription management, payload formatting, VAPID encryption, and robust error handling for delivery failures. It operates as part of a larger system that monitors multiple data sources (SuperSet portal, email notifications, official websites) and distributes notifications through various channels to registered users.
The Web Push Service is organized within the application’s modular architecture, following service-oriented design principles with clear separation of concerns:
Diagram sources
Section sources
The Web Push Service consists of several interconnected components that work together to provide reliable browser notification delivery:
WebPushService Class#
The primary service class that implements the INotificationChannel protocol, handling all aspects of web push notification delivery including VAPID authentication, subscription management, and payload formatting.
VAPID Authentication System#
Implements Voluntary Application Server Identification for secure push notification delivery, requiring cryptographic keys and contact email for authentication with push providers.
Subscription Management#
Manages user subscriptions through the database service, storing subscription endpoints and cryptographic keys associated with each user’s browser installations.
Notification Delivery Pipeline#
Processes notification requests through a structured pipeline that formats payloads, applies VAPID signatures, and handles delivery failures with automatic cleanup of invalid subscriptions.
Section sources
The Web Push Service operates within a comprehensive notification architecture that supports multiple delivery channels:
Diagram sources
The architecture ensures seamless integration between web browsers and the notification system, with automatic handling of subscription lifecycle management and delivery failures.
Section sources
VAPID Encryption Implementation#
The Web Push Service implements VAPID (Voluntary Application Server Identification) encryption for secure notification delivery:
Diagram sources
The VAPID implementation includes:
Private Key Management: Cryptographic key for signing push requests
Public Key Distribution: Shared with web clients for subscription verification
Claims Generation: Creation of authentication claims with contact email
Signature Validation: Verification of push provider authentication
Section sources
Browser Subscription Management#
The service manages browser subscriptions through a structured lifecycle:
Diagram sources
Subscription management includes:
Registration: Storing subscription endpoints and cryptographic keys
Validation: Periodic verification of subscription validity
Cleanup: Automatic removal of expired or invalid subscriptions
Persistence: Database storage of subscription data linked to user profiles
Section sources
Push Notification Delivery Mechanisms#
The notification delivery pipeline follows a standardized process:
Return Success"] ConfigOK --> |Yes| LoadSubscriptions["Load User Subscriptions"] LoadSubscriptions --> HasSubs{"Has Subscriptions?"} HasSubs --> |No| ReturnSuccess["Return Success
No Subscriptions"] HasSubs --> |Yes| FormatPayload["Format Notification Payload"] FormatPayload --> SignVAPID["Apply VAPID Signature"] SignVAPID --> SendPush["Send to Push Provider"] SendPush --> CheckResponse{"Delivery Success?"} CheckResponse --> |Yes| Success["Increment Success Count"] CheckResponse --> |No| CheckStatus{"HTTP 404/410?"} CheckStatus --> |Yes| RemoveSub["Remove Invalid Subscription"] CheckStatus --> |No| LogError["Log Error"] RemoveSub --> NextSub["Next Subscription"] LogError --> NextSub Success --> NextSub NextSub --> MoreSubs{"More Subscriptions?"} MoreSubs --> |Yes| SendPush MoreSubs --> |No| ReturnResults["Return Delivery Statistics"] SkipDelivery --> End([End]) ReturnSuccess --> End ReturnResults --> End
Diagram sources
Section sources
Integration with Multi-Channel Notification Ecosystem#
The Web Push Service integrates with the broader notification ecosystem:
Diagram sources
Section sources
The Web Push Service relies on several key dependencies for secure and reliable operation:
Diagram sources
The dependency graph reveals a sophisticated stack designed for production reliability:
pywebpush: Core library for Web Push protocol implementation
py-vapid: VAPID signature generation and validation
cryptography: Underlying cryptographic operations
http-ece: HTTP Encrypted Content Encoding for payload encryption
FastAPI/uvicorn: Modern asynchronous web framework for API endpoints
Section sources
The Web Push Service is designed with several performance optimizations:
Asynchronous Processing#
Non-blocking Operations: Webhook server uses FastAPI with async capabilities
Connection Pooling: Efficient database connections through PyMongo
Batch Processing: Multiple subscriptions processed in parallel where safe
Resource Management#
Lazy Loading: Optional dependency loading prevents runtime errors
Memory Efficiency: Payload truncation and streaming where applicable
Connection Reuse: Database connections maintained throughout service lifetime
Scalability Features#
Graceful Degradation: Service continues operating even without VAPID keys
Error Isolation: Individual subscription failures don’t impact others
Automatic Cleanup: Invalid subscriptions removed to prevent performance degradation
Section sources
Common VAPID Configuration Issues#
Problem: Web push notifications not working despite proper setup Solution: Verify VAPID key configuration and provider compatibility
Problem: Subscription registration failing with HTTP 501 Solution: Check VAPID key availability and service initialization
Problem: Delivery failures with 404/410 responses Solution: Automatic cleanup removes invalid subscriptions; verify subscription validity
Subscription Management Issues#
Problem: Subscriptions not persisting in database Solution: Verify database connectivity and user ID mapping
Problem: Duplicate subscriptions accumulating Solution: Implement subscription deduplication logic in database service
Performance Issues#
Problem: Slow notification delivery Solution: Monitor database query performance and optimize subscription retrieval
Problem: Memory leaks during high-volume delivery Solution: Implement proper resource cleanup and connection pooling
Section sources
The Web Push Service represents a robust implementation of browser notification delivery within the SuperSet Telegram Notification Bot ecosystem. Its architecture demonstrates best practices in service-oriented design, security implementation, and scalability considerations.
The service successfully bridges the gap between traditional Telegram notifications and modern web browser capabilities, providing users with flexible notification delivery options. The VAPID implementation ensures secure communication with push providers, while the subscription management system maintains clean, valid subscription lists.
Key strengths of the implementation include:
Security: Comprehensive VAPID encryption and authentication
Reliability: Graceful degradation and automatic error recovery
Scalability: Efficient batch processing and resource management
Integration: Seamless coordination with the broader notification ecosystem
Future enhancements could include WebSocket support for real-time bidirectional communication, expanded browser compatibility testing, and advanced analytics for delivery performance monitoring.
Configuration Requirements#
The Web Push Service requires specific environment variables for proper operation:
| Variable | Description | Required | Example |
|---|---|---|---|
| VAPID_PRIVATE_KEY | Private key for VAPID authentication | Yes | -----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY----- |
| VAPID_PUBLIC_KEY | Public key distributed to clients | Yes | BO4... |
| VAPID_EMAIL | Contact email for VAPID | Yes | admin@example.com |
API Endpoints#
The webhook server exposes several endpoints for web push functionality:
| Endpoint | Method | Description |
|---|---|---|
/api/push/subscribe |
POST | Register new push subscription |
/api/push/unsubscribe |
POST | Remove existing subscription |
/api/push/vapid-key |
GET | Retrieve VAPID public key |
/api/notify/web-push |
POST | Send notification via web push only |
Browser Compatibility#
The service supports major modern browsers with Web Push API support:
Chrome: Version 50+
Firefox: Version 44+
Safari: Version 11.3+
Edge: Version 79+
Section sources